<template>
  <MkDialogEdit ref="dialogRef" v-model:isEdit="ctx.isEdit" @submit="submit">
    <MkForm :model="ctx.entity" ref="formRef" formId="BasXueYePinZhong">
      <MkSelectEnum label="血液大类" v-model="ctx.entity.daLei" prop='daLei' :isEdit="ctx.isEdit" />
      <MkInput label="名称" v-model="ctx.entity.mingCheng" prop='mingCheng' :isEdit="ctx.isEdit" required />
      <MkInput label="单位" v-model="ctx.entity.danWei" prop='danWei' :isEdit="ctx.isEdit" required />
      <MkInput label="转换率" v-model="ctx.entity.zhuanHuanLv" prop='zhuanHuanLv' :isEdit="ctx.isEdit" type="number" />
      <MkInputBoolean label="是否配血" v-model="ctx.entity.shiFouPeiXue" prop='shiFouPeiXue' :isEdit="ctx.isEdit" />
      <MkInputBoolean label="是否大量用血" v-model="ctx.entity.shiFouDaLiangYongXue" prop='shiFouDaLiangYongXue'
        :isEdit="ctx.isEdit" />
      <MkInput label="有效期小时数" v-model="ctx.entity.youXiaoQiXiaoShiShu" prop='youXiaoQiXiaoShiShu'
        :isEdit="ctx.isEdit" />
      <MkInput label="排序" v-model="ctx.entity.orderNo" prop='orderNo' :isEdit="ctx.isEdit" />
      <MkInput label="备注" v-model="ctx.entity.remark" prop='remark' :isEdit="ctx.isEdit" />
      <MkIsEnabled label='是否启用' v-model='ctx.entity.isEnabled' prop='isEnabled' :isEdit='ctx.isEdit' />
    </MkForm>
  </MkDialogEdit>
</template>

<script lang='ts' setup>
import { BasXueYePinZhongApi, BasXueYePinZhongOutput } from '/@/api/bas/BasXueYePinZhong';
import MkForm from '/@/componentsMaike/forms/MkForm.vue';
import { isNull, clog } from '/@/utils';

const emits = defineEmits(['query'])
const ctx = reactive({
  isEdit: false,
  entity: {} as BasXueYePinZhongOutput,
});

const dialogRef = ref();
const formRef = ref<InstanceType<typeof MkForm>>();

/** 打开弹框 */
const open = async (row: BasXueYePinZhongOutput) => {
  dialogRef.value.show();
  if (isNull(row?.id)) {
    ctx.isEdit = true;
    ctx.entity = { isEnabled: true } as BasXueYePinZhongOutput;
  } else {
    ctx.isEdit = false;
    ctx.entity = JSON.parse(JSON.stringify(row));
  }
};

const close = () => {
  dialogRef.value?.close();
  emits('query')
};

const submit = async () => {
  if (!ctx.isEdit) {
    close();
    return;
  }
  // 表单验证
  const validate = await formRef?.value?.validate();
  if (!validate) return;
  if (isNull(ctx.entity.id)) {
    await BasXueYePinZhongApi.add(ctx.entity);
  } else {
    await BasXueYePinZhongApi.update(ctx.entity);
  }
  close();
};

defineExpose({ open })
</script>
